/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.common.documentoperate.controller;

import com.icinfo.cloud.common.core.Validator.BaseGroup;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.documentoperate.dto.*;
import com.icinfo.cloud.provider.punish.common.documentoperate.service.IStageDocCommonOperateService;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.CorrectionDocSaveVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonFormDataVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonFormHtmlVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import com.icinfo.framework.tools.utils.MapUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 描述：文书阶段公共操作前端控制器
 *
 * @author xiangyuyu
 * @date 2022-02-16 20:53
 */
@RestController
@RefreshScope
@RequestMapping("/punish/stagedoc/common")
@Slf4j
public class StageDocCommonOperateController extends BaseController {
    @Resource
    private IStageDocCommonOperateService stageDocCommonOperateService;

    /**
     * 【V1.0.0】描述：保存文书信息通用接口
     *
     * @param saveDto {@link StageDocCommonOperateSaveDto}
     * @return {@link Result<String>} 保存结果
     * @author xiangyuyu
     * @date 2022-02-16 20:58
     */
    @PostMapping("/saveDocInfo")
    @ApiOperation(value = "【V1.0.0】描述：保存文书信息通用接口", httpMethod = "POST")
    public Result< Map<String, Object> > saveDocInfo(@RequestBody @Validated StageDocCommonOperateSaveDto saveDto) {
        Map<String, Object> resultMap = stageDocCommonOperateService.saveDocInfo(saveDto);
        String status = MapUtils.getString(resultMap, "success", "");
        if ("true".equals(status)) {
            return Result.success(Constant.STRING_SAVE_SUCCESS,resultMap);
        }
        return Result.error(Constant.STRING_SAVE_FAIL);
    }

    /**
     * 描述：保存补正/更正文书信息
     *
     */
    @PostMapping("/saveCorrectionDocInfo")
    public Result<CorrectionDocSaveVo> saveCorrectionDocInfo(@RequestBody @Validated CorrectionDocSaveDto saveDto) {
        return Result.success(stageDocCommonOperateService.saveCorrectionDocInfo(saveDto));
    }

    /**
     * 描述：保存补正/更正送达文书信息
     *
     */
    @PostMapping("/saveCorrectionConfirmSend")
    public Result saveCorrectionConfirmSend(@RequestBody @Validated CorrectionDocSaveDetailDto saveDto) {
        stageDocCommonOperateService.saveCorrectionConfirmSend(saveDto);
        return Result.success("保存成功");
    }

    /**
     * 获取文书html公共方法
     *
     * @param request
     * @return {@link Result< String>}
     * @author liyafeng
     * @date 2022/2/18
     */
    @PostMapping("/getDocBaseInfo")
    public Result<DocumentCommonFormHtmlVo> getDocHtml(@RequestBody @Validated(BaseGroup.Query.class) StageDocCommonHtmlRequest request) {
        DocumentCommonFormHtmlVo result = stageDocCommonOperateService.getDocumentFormHtml(request);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }

    /**
     * 获取文书数据公共方法
     *
     * @param request
     * @return {@link Result<String> }
     * @author shishengyao
     * @date 2022/02/25
     */
    @PostMapping("/getDocFormData")
    public Result<DocumentCommonFormDataVo> getDocFormData(@RequestBody @Validated(BaseGroup.Query.class) StageDocCommonHtmlRequest request) {
        DocumentCommonFormDataVo result = stageDocCommonOperateService.getCommonDocumentFormData(request);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }

    /**
     * 获取文书数据公共方法(根据documentRecordId)
     *
     * @param request
     * @return {@link Result<String> }
     */
    @PostMapping("/getDocFormDataByDocRecordId")
    public Result<DocumentCommonFormDataVo> getDocFormDataByDocRecordId(@RequestBody @Validated(BaseGroup.Query.class) StageDocCommonHtmlRequest request) {
        DocumentCommonFormDataVo result = stageDocCommonOperateService.getDocFormDataByDocRecordId(request);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }

    /**
     * 文号测试使用，先勿删除
     *
     * @param caseId
     * @param catalogCode
     * @return
     */
    @PostMapping("/testNumber")
    public Result<String> testNumber(@RequestParam("caseId") String caseId, @RequestParam("catalogCode") String catalogCode) {
        Map<String,Object> resultMap = stageDocCommonOperateService.getDocumentNumber(caseId, catalogCode, null);
        String number = resultMap.get("documentNumber").toString();
        return Result.success(number);
    }

    /**
     * 重新生成结案报告文书
     *
     * @param paramJson param json
     * @return {@link Result<String> }
     * @author shishengyao
     * @date 2022/04/24
     */
    @PostMapping("/rebuildPdf")
    public Result<Map<String, Object>> rebuildPdf(@RequestBody Map<String, Object> paramJson) {
        Result<Map<String, Object>> result = stageDocCommonOperateService.rebuildPdf(paramJson);
        return result;
    }

    /**
     * 预览文书接口
     *
     * @author: zhengqiang
     * @date: 2022/5/11
     * @param: previewDto
     * @return {@link Result< Map< String, Object>>}
     **/
    @PostMapping(value = "/docPreview")
    public Result<Map<String, Object>> docPreview(@RequestBody StageDocCommonPreviewDto previewDto) {
        Map<String, Object> map = stageDocCommonOperateService.genPreviewDoc(previewDto);
        return Result.success(map);
    }

    /**
     * 【V1.3.2】描述：根据文书目录编码获取法人或个人文件模板code
     *
     * @param dto {@link TemplateCodeInfoDto}
     * @return Result<String> 模板编号
     * @author xiangyuyu
     * @date 2022/8/9
     * @since 1.3.2
     */
    @PostMapping("/getTemplateCodeByDocumentCatalogCode")
    @ApiOperation(value = "【V1.3.2】描述：根据文书目录编码获取法人或个人文件模板code", httpMethod = "POST")
    public Result<String> getTemplateCodeByDocumentCatalogCode(@Validated @RequestBody TemplateCodeInfoDto dto) {
        String documentCatalogCode = dto.getDocumentCatalogCode();
        String caseId = dto.getCaseId();
        Map<String, Object> paramMap = dto.getParamMap();
        String templateCode = stageDocCommonOperateService.getTemplateCodeByDocumentCatalogCode(documentCatalogCode, caseId, paramMap,"");
        return Result.success(Constant.STRING_GET_SUCCESS, templateCode);
    }

    /**
     * 描述：获取案件相关文书文号
     *
     * @param caseId
     * @param documentCatalogCode 文件目录的代码
     * @return {@link Result<String> }
     * @author shishengyao
     * @date 2023/02/27
     */
    @GetMapping("/getDocumentNumber")
    public Result<String> getDocumentNumber(@RequestParam("caseId") String caseId, @RequestParam("documentCatalogCode") String documentCatalogCode) {
        Map<String, Object> resultMap = stageDocCommonOperateService.getDocumentNumber(caseId, documentCatalogCode, null);
        return Result.success(Constant.STRING_GET_SUCCESS,resultMap.get("documentNumber").toString());
    }

    /**
     * 获取APP直通车文书文号
     * @param busiId
     * @param documentKindCode
     * @param lineCode
     * @return
     */
    @GetMapping("/getDocumentNumberForAppCar")
    public Result<String> getDocumentNumberForAppCar(@RequestParam("busiId") String busiId, @RequestParam("documentKindCode") String documentKindCode, @RequestParam("lineCode") String lineCode, @RequestParam("lineTypeCode") String lineTypeCode) {
        String number = stageDocCommonOperateService.getDocumentNumberForAppCar(busiId, documentKindCode, lineCode, lineTypeCode);
        return Result.success(Constant.STRING_GET_SUCCESS,number);
    }
}
